/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "ui容器部件.h"


static uint32 g分割框手柄变换ID = 0;
static uint32 g分割框手柄颜色ID = 0;

static uint16 g滚动条变换绘制ID = 0;
static uint16 g滚动条颜色绘制ID = 0;



static float32 g手柄宽度 = 6;
static bool		g是否有手柄在拖动;

static vec2 手柄鼠标局部位置 = { 0,0 };








static void f_滚动框布局(C_Widget* self, C_Widget** w, uint32 num) {
	C_滚动框* 滚动框 = dynamic_cast<C_滚动框*>(self);

	vec2 size = 滚动框->m_Size;

	f_布局_双向滚动条(self, E_方向::e_纵向, 3);
	f_布局_双向滚动条(self, E_方向::e_横向, 3);
	
	if (滚动框->m_View->m_扩展视口属性) {
		滚动框->m_View->f_setSize(self->m_Size);
		//滚动框->m_View->m_扩展视口属性->m_边界 = self->m_Size;

		f_Widget_应用视口缩放(滚动框->m_View, 滚动框->m_View);

		//================ 中心偏移比例正常化 ==================
		f_Widget_计算视口缩放保持偏移比例(滚动框->m_View);
	}
	else {
		if (滚动框->m_横向滚动条 && 滚动框->m_纵向滚动条) {
			滚动框->m_View->f_setSize(vec_Max(滚动框->f_getSize(), 滚动框->m_View->m_MinSize));
		}
		else {
			if (滚动框->m_横向滚动条) {
				float32 w = DEF_Max(滚动框->m_Size.x, 滚动框->m_View->m_Size.x);
				if (滚动框->m_父部件) w = DEF_Min(滚动框->m_父部件->m_Size.x, w);
				滚动框->f_setW(w);

			}
			else {
				滚动框->m_View->f_setW(滚动框->m_Size.x - 滚动框->m_滚动条厚度);
			}

			if (滚动框->m_纵向滚动条) {
				float32 h = DEF_Max(滚动框->m_Size.y, 滚动框->m_View->m_Size.y);
				//if(滚动框->m_父部件) h = DEF_Min(滚动框->m_父部件->m_Size.y, h);
				//滚动框->m_View->f_setH(h);
				//滚动框->m_View->f_setW(self->m_Size.x);
				//滚动框->m_View->f_setH(滚动框->m_Size.y);
			}
			else {
				滚动框->m_View->f_setH(滚动框->m_Size.y - 滚动框->m_滚动条厚度);
				//滚动框->f_setW(self->m_Size.x);
			}

		}
		
		
	}
}

static void f_滚动框Draw(C_Widget* self, S_2D画布* 画布) {
	C_滚动框* box = dynamic_cast<C_滚动框*>(self);
	S_凸边图元 图元;
	
	
	box->m_背景绘制.m_属性 = f_vg_gen属性(画布);
	图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);
	box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);

}

static void f_set滚动框变换修改(C_Widget* self) {
	
	C_滚动框* box = dynamic_cast<C_滚动框*>(self);
	box->m_背景绘制.f_alloc(1);

	mat3X2 mat;
	mat.row0 = {};
	mat.row1 = self->m_Size;
	mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };
	box->m_背景绘制.f_set单色填充(0, mat, box->m_颜色A, S_Widget预设图元::m_矩形);

}


static bool on_滚动视口(C_Widget* self, const S_鼠标& 鼠标) {
	C_滚动框* s = dynamic_cast<C_滚动框*>(self);

	//s->m_View->m_Pos.y += 鼠标.lZ;
	f_Widget_应用视口滚动(self, s->m_View, E_方向::e_纵向);
	f_widget_单个部件裁剪(s->m_View, s->m_裁剪大小, s->m_GlobalLoc);

	if (s->m_横向滚动条) s->m_横向滚动条->f_从绑定部件更新滑块();
	if (s->m_纵向滚动条) s->m_纵向滚动条->f_从绑定部件更新滑块();

	self->m_绘制属性更新 = true;
	return true;
}




// : C_Widget(&ctx)
C_滚动框::C_滚动框(S_UI创建环境& ctx, bool 横向滚动条, bool 纵向滚动条, C_Widget* view) {
	//m_纵向滚动条 = 纵向滚动条;
	mf_布局Fun = f_滚动框布局;
	mf_DrawFun = f_滚动框Draw;
	mf_变换修改 = f_set滚动框变换修改;
	mf_鼠标滚轮 = on_滚动视口;

	
	if (view) {
		m_View = view;
	}
	else {
		m_View = new C_Widget(&ctx);
		m_View->f_setSize(f_getSize());
	}

	
	f_添加子组件(m_View);

	m_滚动条厚度 = 10;


	if (横向滚动条 && 纵向滚动条) {
		m_双向滚动条 = new S_双向滚动条(m_滚动条厚度, this, ctx);

		m_双向滚动条->m_H->f_映射值指针(&m_View->m_Pos.x);
		m_双向滚动条->m_V->f_映射值指针(&m_View->m_Pos.y);

		m_双向滚动条->m_H->f_set值范围({ 0, m_View->m_Size.x });
		m_双向滚动条->m_V->f_set值范围({ 0, m_View->m_Size.y });
	}

	if (横向滚动条) {
		m_横向滚动条 = new C_滚动条(ctx, E_方向::e_横向);
		m_横向滚动条->f_映射值指针(&m_View->m_Pos.x);
		m_横向滚动条->f_set值范围({ 0, m_View->m_Size.x });
		f_添加子组件(m_横向滚动条);
	}
	else {
		m_横向滚动条 = nullptr;
	}
	if (纵向滚动条) {
		m_纵向滚动条 = new C_滚动条(ctx, E_方向::e_纵向);
		m_纵向滚动条->f_映射值指针(&m_View->m_Pos.y);
		m_纵向滚动条->f_set值范围({ 0, m_View->m_Size.y });
		f_添加子组件(m_纵向滚动条);
	}
	else {
		m_纵向滚动条 = nullptr;
	}

	m_颜色A = S_UI主题::uic_深色;
}

C_滚动框::~C_滚动框() {
}



C_Widget* f_ui_创建滚动框(S_UI创建环境& ctx, bool 横向滚动条, bool 纵向滚动条, C_Widget* view) {
	C_Widget* w = new C_滚动框(ctx, 横向滚动条, 纵向滚动条, view);
	return w;
}

void f_ui_滚动框安装视口组件(C_Widget* 滚动框, C_Widget* view) {
	C_滚动框* box = dynamic_cast<C_滚动框*>(滚动框);
	box->m_子组件[0] = view;
	box->m_View = view;

	view->m_父部件 = box;
}








E_事件是否传递 f_分割框手柄准备拖动(struct C_Widget* 部件, const S_鼠标& 鼠标, S_拖放数据& data) {
	C_分割框手柄* 手柄 = dynamic_cast<C_分割框手柄*>(部件);
	auto* 分割框 = dynamic_cast<C_分割框*>(手柄->m_父部件);
	//std::cout << 手柄->m_Pos.x << " " << value.y << std::endl;
	手柄鼠标局部位置 = f_widget_get鼠标局部位置(鼠标.gPos, 部件);
	
	if (分割框) {
		if (分割框->m_排列方向 == E_方向::e_横向) {
			C_Widget::g_鼠标样式 = E_鼠标样式::e_鼠标样式_纵向双箭头;
		}
		else {
			C_Widget::g_鼠标样式 = E_鼠标样式::e_鼠标样式_横向双箭头;
		}
	}

	g是否有手柄在拖动 = true;
	return E_事件是否传递::e_事件传递_继续;
}

E_事件是否传递 f_分割框手柄拖动(struct C_Widget* 部件, const S_鼠标& 鼠标, S_拖放数据& data) {
	C_分割框手柄* 手柄 = dynamic_cast<C_分割框手柄*>(部件);
	auto* 分割框 = dynamic_cast<C_分割框*>(手柄->m_父部件);
	//std::cout << value.x << " " << value.y << std::endl;
	vec2 pos = f_widget_get鼠标局部位置(鼠标.gPos, 手柄->m_父部件);
	//std::cout << pos.x << pos.y << std::endl;
	switch (分割框->m_排列方向)
	{
	case E_方向::e_横向:
		分割框->f_分割条移动(pos.y - 手柄鼠标局部位置.y + g手柄宽度 * 0.5, 手柄->m_分割条ID);
		break;
	case E_方向::e_纵向:
		分割框->f_分割条移动(pos.x - 手柄鼠标局部位置.x + g手柄宽度*0.5, 手柄->m_分割条ID);
		/*分割框->m_分割比例[手柄->m_分割条ID] = float32(pos.x - 手柄鼠标局部位置.x) / 分割框->m_Size.x;
		分割框->m_绘制属性更新 = true;

		分割框->m_子组件[手柄->m_分割条ID * 2]->m_绘制属性更新 = true;
		分割框->m_子组件[手柄->m_分割条ID * 2 + 2]->m_绘制属性更新 = true;

		手柄->f_setMove({ pos.x - 手柄鼠标局部位置.x, 0 });*/
		break;
	}

	return E_事件是否传递::e_事件传递_继续;
}

bool on_分割框手柄结束拖放(C_Widget* self, C_Widget* 拾取的部件, S_拖放数据& data) {
	C_Widget::g_鼠标样式 = E_鼠标样式::e_鼠标样式_默认;
	g是否有手柄在拖动 = false;
	return true;
}



C_分割框手柄::C_分割框手柄(S_UI创建环境& ctx) {
	f_setSize(vec2{4});
	m_分割条ID = 0;

	mf_开始拖拽 = f_分割框手柄准备拖动;
	mf_拖拽 = f_分割框手柄拖动;
	mf_结束拖拽 = on_分割框手柄结束拖放;

	m_TypeName = "分割框手柄";
}











static void f_分割框布局(C_Widget* self, C_Widget** w, uint32 num) {
	auto* 分割框 = dynamic_cast<C_分割框*>(self);

	uint32 子组件数量 = self->m_子组件.size();
	if (子组件数量 == 0) return;

	if (子组件数量 >= 3) {
		子组件数量 /= 2;
		++子组件数量;
	}


	auto* 分割比例	= 分割框->m_分割比例.data();
	auto* p			= 分割框->m_子组件.data();


	switch (分割框->m_排列方向) {
	case E_方向::e_横向:
	{
		for (uint16 i = 0; i < 子组件数量; ++i) {
			uint16 id = i * 2;

			vec2 pos = { 0.0, 0.0 };
			vec2 size;
			if (i) {
				pos.y = 分割比例[i - 1] * self->m_Size.y - g手柄宽度 * 0.5;
				size = { self->m_Size.x, g手柄宽度 };

				//手柄设置
				p[id - 1]->f_setPos(pos);
				p[id - 1]->m_Size = size;

				pos.y = 分割比例[i - 1] * self->m_Size.y + g手柄宽度 * 0.5;
				size.y = (分割比例[i] - 分割比例[i - 1]) * self->m_Size.y;
			}
			else {
				pos.x = 0;
				size = { self->m_Size.x, 分割比例[i] * self->m_Size.y - g手柄宽度 * 0.5f };
			}

			//部件设置
			p[id]->f_setPos(pos);
			p[id]->f_setSize(size);
		}
	}

	break;
	case E_方向::e_纵向:
	{
		for (uint16 i = 0; i < 子组件数量; ++i) {
			uint16 id = i * 2;

			vec2 pos = { 0.0, 0.0 };
			vec2 size;
			if (i) {
				pos.x = 分割比例[i - 1] * self->m_Size.x - g手柄宽度 * 0.5;
				size = { g手柄宽度, self->m_Size.y };

				//手柄设置
				p[id - 1]->f_setPos(pos);
				p[id - 1]->m_Size = (size);

				size.x = (分割比例[i] - 分割比例[i - 1]) * self->m_Size.x;
				pos.x += g手柄宽度;
			}
			else {
				pos.x = 0;
				size = { 分割比例[i] * self->m_Size.x - g手柄宽度 * 0.5f, self->m_Size.y };
			}

			//部件设置
			p[id]->f_setPos(pos);
			p[id]->f_setSize(size);

		}
	}
	break;
	}

}

static void f_分割框Draw(struct C_Widget* self, struct S_2D画布* 画布) {
	auto* 分割框 = dynamic_cast<C_分割框*>(self);
	//分割框->m_线绘制.f_Begin_E(1, 4.0);
}

static void f_set变换修改(C_Widget* self, mat3X2** t, uint16 num) {
	uint32 子组件数量 = self->m_子组件.size();
	auto e = self->mf_get更新组件(self);

	if (子组件数量 == 0) return;
	子组件数量 /= 2;

	C_分割框* 分割框 = dynamic_cast<C_分割框*>(self);
	auto* 分割比例 = 分割框->m_分割比例.data();
	auto* p = self->m_子组件.data();

	
	for (uint16 i = 0; i < 子组件数量; ++i) {
		uint16 id = i * 2 + 1;
	}
}


static bool on_分割框手柄鼠标进入(C_Widget* self, const S_鼠标& 轮值) {
	C_分割框* 分割框 = dynamic_cast<C_分割框*>(self->m_父部件);
	if (分割框) {
		if (分割框->m_排列方向 == E_方向::e_横向) {
			C_Widget::g_鼠标样式 = E_鼠标样式::e_鼠标样式_纵向双箭头;
		}
		else {
			C_Widget::g_鼠标样式 = E_鼠标样式::e_鼠标样式_横向双箭头;
		}
	}
	return true;
}

static bool on_分割框手柄鼠标移出(C_Widget* self, const S_鼠标& 轮值) {
	if (!g是否有手柄在拖动) {
		C_Widget::g_鼠标样式 = E_鼠标样式::e_鼠标样式_默认;
	}
	return true;
}

// : C_Widget(&ctx)
C_分割框::C_分割框(S_UI创建环境& ctx, E_方向 方向): m_排列方向(方向) {
	mf_布局Fun = f_分割框布局;
	//mf_变换修改 = f_set变换修改;
	//mf_DrawFun = f_分割框Draw;
	m_TypeName = "分割框";
	m_WName = L"分割框";
}

C_分割框::~C_分割框() {
}


void C_分割框::f_add分割(C_Widget* w, float32 比例) {
	if (m_子组件.size()) {
		C_分割框手柄* 手柄 = new C_分割框手柄(*m_UIctx);
		手柄->m_分割条ID = m_子组件.size() / 2;

		手柄->mf_鼠标进入 = on_分割框手柄鼠标进入;
		手柄->mf_鼠标离开 = on_分割框手柄鼠标移出;

		f_添加子组件(手柄);
	}
	f_添加子组件(w);
	m_分割比例.push_back(比例);
	
}

void C_分割框::f_remove分割(int8 dir) {
}



void C_分割框::f_分割条移动(float32 局部位置, int16 分割条ID) {
	C_分割框手柄* 手柄 = dynamic_cast<C_分割框手柄*>(m_子组件[分割条ID * 2 + 1]);


	vec2 pos = {};
	vec2 size = m_Size;

	if (局部位置 < g手柄宽度) 局部位置 = g手柄宽度;
	//float32 原比例 = m_分割比例[手柄->m_分割条ID];
	switch (m_排列方向) {
		case E_方向::e_横向:
		{
			m_分割比例[手柄->m_分割条ID] = 局部位置 / m_Size.y;
			//m_分割比例[手柄->m_分割条ID + 1] += m_分割比例[手柄->m_分割条ID] - 原比例;
			
			pos = { 0.0f, m_分割比例[手柄->m_分割条ID] * m_Size.y - g手柄宽度 * 0.5f };
			手柄->f_setPos(pos);
			
			//控制柄前一个部件
			if (手柄->m_分割条ID) {
				size.y = (m_分割比例[手柄->m_分割条ID] * m_Size.y) - g手柄宽度 * 0.5f;
			}
			else {
				size.y = pos.y; 
				pos.y = 0;
			}
			m_子组件[2 * 手柄->m_分割条ID]->f_setSize(size);


			//控制柄后一个部件
			pos.y = m_分割比例[手柄->m_分割条ID] * m_Size.y + g手柄宽度 * 0.5f;
			size.y = (m_分割比例[手柄->m_分割条ID + 1] - m_分割比例[手柄->m_分割条ID]) * m_Size.y;

			m_子组件[2 * 手柄->m_分割条ID + 2]->f_setPos(pos);
			m_子组件[2 * 手柄->m_分割条ID + 2]->f_setSize(size);
		}
		break;
		case E_方向::e_纵向:
		{
			m_分割比例[手柄->m_分割条ID] = 局部位置 / m_Size.x;


			pos = { m_分割比例[手柄->m_分割条ID] * m_Size.x - g手柄宽度 * 0.5f, 0.0f };
			//if (pos.x < g手柄宽度) pos.x = g手柄宽度;
			手柄->f_setPos(pos);

			if (手柄->m_分割条ID) {
				size.x = (m_分割比例[手柄->m_分割条ID] - m_分割比例[手柄->m_分割条ID - 1]) * m_Size.x - g手柄宽度;
			}
			else {
				size.x = pos.x;
				pos.x = 0;
			}

			m_子组件[2 * 手柄->m_分割条ID]->f_setSize(size);



			pos.x = m_分割比例[手柄->m_分割条ID] * m_Size.x + g手柄宽度 * 0.5;
			size.x = (m_分割比例[手柄->m_分割条ID + 1] - m_分割比例[手柄->m_分割条ID]) * m_Size.x - g手柄宽度;

			m_子组件[2 * 手柄->m_分割条ID + 2]->f_setPos(pos);
			m_子组件[2 * 手柄->m_分割条ID + 2]->f_setSize(size);
		}
		break;
	}

	//m_分割比例[手柄->m_分割条ID] = 局部位置 / m_Size.x;
	m_绘制属性更新 = true;

}

std::vector<C_Widget*>	f_ui_get分割框页面组件(C_Widget* self) {
	std::vector<C_Widget*> ws;

	auto e = self->f_get更新部件();
	uint32 num = e.m_Num;
	for (uint32 i = 0; i < num; ++i) {
		if(!(i&0x00000001)) ws.emplace_back(e.m_W[i]);
	}

	return ws;
}




















static void f_多页面Draw(C_Widget* self, struct S_2D画布* 画布) {
	C_多页面* mpag = dynamic_cast<C_多页面*>(self);

	mpag->m_背景绘制.m_属性 = f_vg_gen属性(画布);

	S_凸边图元 图元{};
	图元.属性 = f_vg_get属性(画布, mpag->m_背景绘制.m_属性);

	mpag->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);

	auto 字体 = f_surface_get字体(*self->m_UIctx, S_UI主题::ui_默认字体);
	mpag->m_标签绘制.f_Draw(画布, &字体, mpag->m_背景绘制.m_属性);
	mpag->m_图标绘制.f_Draw单色(画布, mpag->m_背景绘制.m_属性, S_UI主题::ui_默认图标);

}

static void f_多页面布局(C_Widget* self, C_Widget** w, uint32 num) {
	if (!num) return;

	C_多页面* mpag = dynamic_cast<C_多页面*>(self);
	
	vec2 pos = {4, 4};
	vec2 size = mpag->m_Size;
	
	if (mpag->m_排列方向 == E_方向::e_纵向) {
		for (uint32 i = 1; i < num; ++i) {
			w[i]->f_setPos(pos);
			w[i]->f_setSize(w[i]->m_Size);

			if (mpag->m_可关闭) {

			}

			pos.y += mpag->m_头大小.y + 4;
		}

		pos = { mpag->m_头大小.x, 0 };
		size.x -= mpag->m_头大小.x;
		
		w[0]->f_setPos(pos);
		w[0]->f_setSize(size);
	}
	else {
		for (uint32 i = 1; i < num; ++i) {
			w[i]->f_setPos(pos);
			w[i]->f_setSize(w[i]->m_Size);

			if (mpag->m_可关闭 && w[i]->m_子组件.size()) {
				vec2 关闭按钮大小 = w[i]->m_Size;
				关闭按钮大小.x = DEF_Min(关闭按钮大小.x, 关闭按钮大小.y);
				关闭按钮大小.x = 关闭按钮大小.y;
				w[i]->m_子组件[0]->f_setPos({1,1});
				w[i]->m_子组件[0]->f_setSize(关闭按钮大小 - 2);
			}

			pos.x += mpag->m_头大小.x + 4;
		}

		pos = { 0, mpag->m_头大小.y };
		size.y -= mpag->m_头大小.y;
		
		w[0]->f_setPos(pos);
		w[0]->f_setSize(size);
	}

}

static void f_多页面变换修改(C_Widget* self) {
	C_多页面* mpag = dynamic_cast<C_多页面*>(self);
	uint32 标签数量 = mpag->m_分页面.size();
	

	auto e = mpag->f_get更新部件();
	//uint32 num = self->f_get子部件数量();
	uint32 num = 标签数量 + 2;

	#define 每个标签文字数量 10
	mpag->m_背景绘制.f_alloc(标签数量 + 2);
	mpag->m_标签绘制.f_alloc(标签数量, 每个标签文字数量);

	

	if (mpag->m_可关闭) {
		mpag->m_图标绘制.f_alloc(标签数量);
	}

	//if (!标签数量) return;

	mat3X2 mat;
	if (mpag->m_排列方向 == E_方向::e_纵向) {
		for (uint32 i = 1; i < num; ++i) {
			mat.row0 = e.m_W[i]->m_Pos;
			mat.row1 = e.m_W[i]->m_Size;
			mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };
			mpag->m_背景绘制.f_set单色填充(i, mat, e.m_W[i]->m_颜色A, S_Widget预设图元::m_上圆角矩形);

			mpag->m_标签绘制.f_居中绘制文本({ mat.row0, mat.row1 }, S_UI主题::uic_文字颜色, i, L"mbt");
		}
	}
	else {
		mat.row0 = {};
		mat.row1 = { self->m_Size.x, mpag->m_头大小.y };
		mat.row2 = { S_UI主题::ui_面板圆角半径*2, 0 };
		mpag->m_背景绘制.f_set单色填充(0, mat, S_UI主题::uic_页面标签背景, S_Widget预设图元::m_上圆角矩形);

		for (uint32 i = 1; i < num; ++i) {
			mat.row0 = e.m_W[i]->m_Pos;
			mat.row1 = e.m_W[i]->m_Size;
			mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };
			mpag->m_背景绘制.f_set单色填充(i, mat, e.m_W[i]->m_颜色A, S_Widget预设图元::m_上圆角矩形);

			if(i <= 标签数量)
				mpag->m_标签绘制.f_居中绘制文本({ mat.row0, mat.row1 }, S_UI主题::uic_文字颜色, i - 1, e.m_W[i]->m_WName);

			
			if (mpag->m_可关闭 && i <= 标签数量) {
				mat.row1.x = DEF_Min(mat.row1.x, mat.row1.y) - 2;
				mat.row0 += { mat.row1.x*0.4f, mat.row1.y*0.4f };
				mat.row1.x = mat.row1.y *= 0.5;
				mpag->m_图标绘制.f_set("删除", i-1, mat, S_UI主题::uic_文字颜色);
			}
		}
	}

	mpag->m_背景绘制.f_setColor(mpag->m_当前页面ID+1, e.m_W[mpag->m_当前页面ID + 1]->m_颜色C);

}

static bool on_标签鼠标进入(C_Widget* self, const S_鼠标& 轮值) {
	C_多页面* mpag = dynamic_cast<C_多页面*>(self->m_父部件);
	if (mpag->m_当前页面ID != self->m_实例ID)
		mpag->m_背景绘制.f_setColor(self->m_实例ID+1, self->m_颜色B);
	return true;
}

static bool on_标签鼠标移出(C_Widget* self, const S_鼠标& 轮值) {
	C_多页面* mpag = dynamic_cast<C_多页面*>(self->m_父部件);
	//self->m_颜色A = S_UI主题::uic_按钮背景;
	if(mpag->m_当前页面ID != self->m_实例ID)
		mpag->m_背景绘制.f_setColor(self->m_实例ID+1, self->m_颜色A);
	//	f_vg_set单色(self->m_父部件->m_画布, g多页面GPU绘制参数ID.m_标签颜色ID, self->m_实例ID, self->m_颜色A);
	return true;
}


static bool f_多页面标签选中(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_多页面* mpag = dynamic_cast<C_多页面*>(self->m_父部件);
	uint32 loc = f_widget_取组件位置索引(mpag, self);
	mpag->f_set页面(loc - 1);
	return true;
}

static bool f_标签关闭页面(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_多页面* mpag = dynamic_cast<C_多页面*>(self->m_父部件->m_父部件);

	if (mpag->m_分页面.size() > 1) {
		uint32 loc = f_widget_取组件位置索引(mpag, self->m_父部件);
		mpag->f_del页面(loc - 1);
	}
	
	return true;
}


C_多页面::C_多页面(S_UI创建环境& ctx, E_方向 方向) : C_Widget(&ctx), m_排列方向(方向) {
	m_WName = L"多页面";
	
	mf_布局Fun = f_多页面布局;
	mf_DrawFun = f_多页面Draw;
	mf_变换修改 = f_多页面变换修改;

	m_头大小 = { 60, 20 };
	m_是否延展 = { true, true };
	
	m_页面数量 = 1;
	m_当前页面ID = 0;
	//C_Widget* w = new C_Widget();
	m_添加按钮 = nullptr;
	m_可关闭 = true;
	//f_添加子组件(w);
}

C_多页面::~C_多页面() {
}

void C_多页面::f_add页面(C_Widget* w, int8 loc) {
	uint32 子部件数量 = m_分页面.size();

	w->m_绘制属性更新 = true;
	w->m_显示 = false;
	if (!m_分页面.size()) {
		f_添加子组件(w, 0);
	}
	m_分页面.push_back(w);


	C_Widget* 标签 = new C_Widget();
	标签->mf_鼠标按键点击 = f_多页面标签选中;
	标签->m_实例ID = 子部件数量;
	标签->f_setSize(m_头大小);

	标签->mf_鼠标进入 = on_标签鼠标进入;
	标签->mf_鼠标离开 = on_标签鼠标移出;
	标签->m_颜色A = S_UI主题::uic_页面标签;
	标签->m_颜色B = S_UI主题::uic_页面标签 + S_RGBA8UI{ 25, 25, 25, 255 };
	标签->m_颜色C = S_UI主题::uic_页面焦点标签;

	标签->m_WName = w->m_WName;

	if (m_可关闭) {
		C_Widget* 关闭按钮 = new C_Widget;
		关闭按钮->f_setSize({ 20,20 });
		关闭按钮->m_ICO = "删除";
		关闭按钮->mf_鼠标按键点击 = f_标签关闭页面;
		标签->f_添加子组件(关闭按钮);
	}

	int32 添加位置 = -1;
	if (m_添加按钮) {
		添加位置 = f_get子部件数量() - 1;
	}
	f_添加子组件(标签, 添加位置);



}

void C_多页面::f_add页面(std::wstring name, int8 loc) {
	uint32 子部件数量 = m_分页面.size();

	C_Widget* 标签 = new C_Widget();
	标签->mf_鼠标按键点击 = f_多页面标签选中;
	标签->m_实例ID = 子部件数量;

	标签->mf_鼠标进入 = on_标签鼠标进入;
	标签->mf_鼠标离开 = on_标签鼠标移出;
	标签->m_颜色A = S_UI主题::uic_按钮背景;
	标签->m_颜色B = S_UI主题::uic_按钮背景 + S_RGBA8UI{ 35, 35, 35, 255 };

	//m_分页面.push_back(标签);
	f_添加子组件(标签, -1);


	if (m_可关闭) {
		C_Widget* 关闭按钮 = new C_Widget;
		关闭按钮->f_setSize({20,20});
		关闭按钮->m_ICO = "删除";
		关闭按钮->mf_鼠标按键点击 = f_标签关闭页面;
		标签->f_添加子组件(关闭按钮);
	}
}

C_Widget* C_多页面::f_del页面(int8 loc) {
	C_Widget* delWidget = m_分页面[loc];
	
	m_分页面.erase(m_分页面.begin() + loc);

	f_widget_remove子部件(this, loc + 1, loc + 2);

	m_当前页面ID = DEF_Min(m_当前页面ID, m_分页面.size() -1);
	return delWidget;
}

void C_多页面::f_set页面(uint32 id) {
	if(m_分页面.size() <= 0) return;
	m_分页面[m_当前页面ID]->m_显示 = false;

	m_当前页面ID = id;

	auto& e = m_分页面[m_当前页面ID];
	if (m_分页面.size()) {
		m_子组件[0]->m_被裁剪 = false;
	}
	if (e->mf_打开关闭) e->mf_打开关闭(e, true);

	e->m_被裁剪 = false;
	f_替换子组件(0, e);
}


E_方向 f_ui_get多页面标签方向(C_Widget* self) {
	return static_cast<C_多页面*>(self)->m_排列方向;
}

std::vector<C_Widget*> f_ui_get多页面所有页面(C_Widget* self) {
	return static_cast<C_多页面*>(self)->m_分页面;
}

C_Widget* f_ui_get选项夹页面(C_Widget* w, uint16 id) {
	auto* page = dynamic_cast<C_多页面*>(w);
	if (page && id < page->m_分页面.size()) {
		return page->m_分页面[id];
	}
	return 0;
}

void f_ui_选项夹是否可关闭(C_Widget* self, bool 可关闭) {
	C_多页面* 选项夹 = static_cast<C_多页面*>(self);

	if (可关闭) {

	}
}











/*bool f_展开栏手柄准备拖动(struct C_Widget* 部件, const S_鼠标& 鼠标, S_拖放数据& data) {
	C_分割框手柄* 手柄 = dynamic_cast<C_分割框手柄*>(部件);
	手柄鼠标局部位置 = f_widget_取鼠标局部位置(鼠标.gPos, 部件);

	return true;
}

bool f_展开栏手柄拖动(struct C_Widget* 部件, const S_鼠标& 鼠标, S_拖放数据& data) {
	C_分割框手柄* 手柄 = dynamic_cast<C_分割框手柄*>(部件);
	auto* 分割框 = dynamic_cast<C_展开栏*>(手柄->m_父部件);
	vec2 pos = f_widget_取鼠标局部位置(鼠标.gPos, 手柄->m_父部件);
	
	return true;
}*/

static void on_展开栏布局(C_Widget* self, C_Widget** w, uint32 num) {
	float32 g2 = self->m_间隔宽度;

	w[0]->f_setW(self->m_Size.x);
	
	w[1]->f_setPos({ g2, w[0]->m_Size.y });
	g2 = self->m_间隔宽度 * 2;
	w[1]->f_setSize({ self->m_Size.x - g2, self->m_Size.y - w[2]->m_Size.y - w[0]->m_Size.y });

	w[2]->f_setPos({ 0, self->m_Size.y - w[2]->m_Size.y });
	w[2]->f_setSize({ self->m_Size.x, w[2]->m_Size.y });

}

C_展开栏::C_展开栏(S_UI创建环境& ctx, C_Widget* 内部组件) {
	assert(内部组件);

	C_Widget*		展开按钮 = new C_Widget();
	C_分割框手柄*	拖动按钮 = new C_分割框手柄(ctx);

	展开按钮->f_setSize({ 20,26 });
	拖动按钮->f_setSize({ 20,20 });

	f_添加子组件(展开按钮);
	f_添加子组件(内部组件);
	f_添加子组件(拖动按钮);

	mf_布局Fun = on_展开栏布局;
}

C_展开栏::~C_展开栏() {
}







static void on_动态分列栏布局(C_Widget* self, C_Widget** w, uint32 num) {
	assert(dynamic_cast<C_动态分列栏*>(self));

	C_动态分列栏* box = (C_动态分列栏*)(self);
	
	if (box->m_排列方向 == E_方向::e_纵向) {
		float32 x = self->m_Size.x - self->m_间隔宽度 * 2;
		float32 y = self->m_间隔宽度;

		
		for (uint32 i = 0; i < num; ++i) {
			w[i]->f_setPos({ float32(self->m_间隔宽度), y });
			
			w[i]->f_setW(x);
			//w[i]->f_setH(256);

			y += w[i]->m_Size.y + self->m_间隔宽度;
		}

		self->f_setH(y);

	}else{

	}
}

C_动态分列栏::C_动态分列栏(S_UI创建环境& ctx)
{
	mf_布局Fun = on_动态分列栏布局;
	m_排列方向 = E_方向::e_纵向;
}

C_动态分列栏::~C_动态分列栏() {
}













void on_树形框项修改PropUpdate(S_Props& prop) {

}


static void on_树形框Draw(C_Widget* self, struct S_2D画布* 画布) {
	C_多页面* mpag = dynamic_cast<C_多页面*>(self);

	mpag->m_背景绘制.m_属性 = f_vg_gen属性(画布);

	S_凸边图元 图元{};
	图元.属性 = f_vg_get属性(画布, mpag->m_背景绘制.m_属性);
	mpag->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);

	//mpag->m_背景绘制.f_Draw点(画布, mpag->m_背景绘制.m_属性, E_笔刷类型::e_条纹);
	auto 字体 = f_surface_get字体(*self->m_UIctx, S_UI主题::ui_默认字体);
	mpag->m_标签绘制.f_Draw(画布, &字体, mpag->m_背景绘制.m_属性);

}

static void on_树形框布局(C_Widget* self, C_Widget** w, uint32 num) {
	if (!num) return;

	C_多页面* mpag = dynamic_cast<C_多页面*>(self);

	vec2 pos = { 4, 4 };
	vec2 size = mpag->m_Size;

	if (mpag->m_排列方向 == E_方向::e_纵向) {
		for (uint32 i = 1; i < num; ++i) {
			w[i]->f_setPos(pos);
			w[i]->f_setSize(w[i]->m_Size);

			pos.y += mpag->m_头大小.y + 4;
		}

		pos = { mpag->m_头大小.x, 0 };
		size.x -= mpag->m_头大小.x;

		w[0]->f_setPos(pos);
		w[0]->f_setSize(size);
	}
	else {
		for (uint32 i = 1; i < num; ++i) {
			w[i]->f_setPos(pos);
			w[i]->f_setSize(w[i]->m_Size);

			pos.x += mpag->m_头大小.x + 4;
		}

		pos = { 0, mpag->m_头大小.y };
		size.y -= mpag->m_头大小.y;

		w[0]->f_setPos(pos);
		w[0]->f_setSize(size);
	}

}

static void on_树形框变换修改(struct C_Widget* self) {
	C_多页面* mpag = dynamic_cast<C_多页面*>(self);
	uint32 标签数量 = mpag->m_分页面.size();
	auto e = mpag->f_get更新部件();
	uint32 num = self->f_get子部件数量();
	return;

	//mpag->m_背景绘制.f_Begin_F(标签数量 + 2);
	mpag->m_标签绘制.f_alloc(标签数量, 10);

	const auto& 字体 = f_surface_get字体库(*self->m_UIctx, S_UI主题::ui_默认字体);

	mat3X2 mat;
	if (mpag->m_排列方向 == E_方向::e_纵向) {
		for (uint32 i = 1; i < num; ++i) {
			mat.row0 = e.m_W[i]->m_Pos;
			mat.row1 = e.m_W[i]->m_Size;
			mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };
			//mpag->m_背景绘制.f_GPU参数修改(i, mat, e.m_W[i]->m_颜色A, S_Widget预设图元::m_上圆角矩形);

			mpag->m_标签绘制.f_居中绘制文本({ mat.row0, mat.row1 }, S_UI主题::uic_文字颜色, i, L"mbt");
		}
	}
	else {
		mat.row0 = {};
		mat.row1 = { self->m_Size.x, mpag->m_头大小.y };
		mat.row2 = { S_UI主题::ui_面板圆角半径 * 2, 0 };
		//mpag->m_背景绘制.f_GPU参数修改(0, mat, S_UI主题::uic_页面标签背景, S_Widget预设图元::m_上圆角矩形);

		for (uint32 i = 1; i < num; ++i) {
			mat.row0 = e.m_W[i]->m_Pos;
			mat.row1 = e.m_W[i]->m_Size;
			mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };
			//mpag->m_背景绘制.f_GPU参数修改(i, mat, e.m_W[i]->m_颜色A, S_Widget预设图元::m_上圆角矩形);

			mpag->m_标签绘制.f_居中绘制文本({ mat.row0, mat.row1 }, S_UI主题::uic_文字颜色, i - 1, e.m_W[i]->m_WName);
		}
	}

	mpag->m_背景绘制.f_setColor(mpag->m_当前页面ID + 1, e.m_W[mpag->m_当前页面ID + 1]->m_颜色C);

}


C_树形框::C_树形框(S_UI创建环境& ctx) {
	mf_DrawFun = on_树形框Draw;
	mf_布局Fun = on_树形框布局;
	mf_变换修改 = on_树形框变换修改;
}

C_树形框::~C_树形框() {
}





C_卡片框::C_卡片框(S_UI创建环境& ctx) : C_Widget(&ctx) {

}

C_卡片框::~C_卡片框() {
}

C_Widget* f_ui_创建卡片框(S_UI创建环境& ctx) {

	return nullptr;
}




